package com.ld.control.business.robot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.control.business.robot.mapper.HuiLingRobotPointVariableMapper;
import com.ld.control.business.robot.model.HuiLingRobotPointVariableModel;
import com.ld.control.business.robot.service.HuiLingRobotPointVariableService;
import com.ld.control.business.robot.vo.req.HuiLingRobotPointVariableReqVo;
import com.ld.control.business.robot.vo.resp.HuiLingRobotPointVariableRespVo;
import com.ld.control.common.vo.GeneratePageListVo;
import com.ld.control.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 机械臂点位信息 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2025-09-12
 */
@Service
public class HuiLingRobotPointVariableServiceImpl extends ServiceImpl<HuiLingRobotPointVariableMapper, HuiLingRobotPointVariableModel> implements HuiLingRobotPointVariableService {

    @Autowired
    private HuiLingRobotPointVariableMapper huiLingRobotPointVariableMapper;

    @Override
    public void add(HuiLingRobotPointVariableReqVo reqVo) {
        addOrUpdateCheck(reqVo);
        HuiLingRobotPointVariableModel huiLingRobotPointVariableModel = new HuiLingRobotPointVariableModel();
        BeanUtils.copyProperties(reqVo, huiLingRobotPointVariableModel);
        huiLingRobotPointVariableMapper.insert(huiLingRobotPointVariableModel);

    }

    @Override
    public void update(HuiLingRobotPointVariableReqVo reqVo) {
        addOrUpdateCheck(reqVo);
        HuiLingRobotPointVariableModel huiLingRobotPointVariableModel = new HuiLingRobotPointVariableModel();
        BeanUtils.copyProperties(reqVo, huiLingRobotPointVariableModel);
        huiLingRobotPointVariableMapper.updateById(huiLingRobotPointVariableModel);
    }

    @Override
    public HuiLingRobotPointVariableRespVo getById(Long id) {
        HuiLingRobotPointVariableRespVo respVo = new HuiLingRobotPointVariableRespVo();
        HuiLingRobotPointVariableModel huiLingRobotPointVariableModel = huiLingRobotPointVariableMapper.selectById(id);
        BeanUtils.copyProperties(huiLingRobotPointVariableModel, respVo);
        return respVo;
    }

    @Override
    public GeneratePageListVo<HuiLingRobotPointVariableRespVo> getPageList(String name, Integer current, Integer size) {
        IPage<HuiLingRobotPointVariableRespVo> iPage = new Page<>(current, size);
        IPage<HuiLingRobotPointVariableRespVo> iPage1 = huiLingRobotPointVariableMapper.selectPageList(iPage, name);
        return new GeneratePageListVo<>(iPage1);
    }

    @Override
    public void deleteById(Long id) {
        huiLingRobotPointVariableMapper.deleteById(id);
    }

    @Override
    public HuiLingRobotPointVariableRespVo getByName(String name) {
        HuiLingRobotPointVariableRespVo respVo = new HuiLingRobotPointVariableRespVo();
        QueryWrapper<HuiLingRobotPointVariableModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HuiLingRobotPointVariableModel.NAME, name);
        HuiLingRobotPointVariableModel huiLingRobotPointVariableModel = huiLingRobotPointVariableMapper.selectOne(queryWrapper);
        BeanUtils.copyProperties(huiLingRobotPointVariableModel, respVo);
        return respVo;
    }


    private void addOrUpdateCheck(HuiLingRobotPointVariableReqVo reaVo) {
        QueryWrapper<HuiLingRobotPointVariableModel> queryWrapper = new QueryWrapper<>();
        if (reaVo.getId() != null) {
            queryWrapper.ne(HuiLingRobotPointVariableModel.ID, reaVo.getId());
        }
        queryWrapper.eq(HuiLingRobotPointVariableModel.NAME, reaVo.getName());
        Long count = huiLingRobotPointVariableMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusinessException("点位变量名称已经存在！");
        }

    }
}
